<?php

namespace app\common\model;

/**
 * [Fu  福券表]
 * @author [C。]
 * @return html
 */
class Fu extends Base
{
    protected $name = 'fu';

    /**
     * 福券列表信息
     */
    public function getlists($map,$from = '', $size = '')
    {
        $order = ['fu_id' => 'desc'];

        $result = $this->where($map)->limit($from, $size)->order($order)->select()->toArray();
        return $result;
    }
    /**
     * 根据搜索获取福券列表信息
     */
    public function getlist($map,$from = '', $size = '')
    {
        $join1 = [
            ['app_member_quan mq','mq.fu_id = f.fu_id'],
        ];

        $join2 = [
            ['app_member am','mq.member_id = am.id'],
        ];

        $order = ['f.fu_id' => 'desc'];

        $result =  $this->distinct(true)->field('f.*,am.tel,am.username,mq.addtime')
            ->alias('f')
            ->join($join1)
            ->join($join2)
            ->where($map)
            ->limit($from, $size)
            ->order($order)
            ->select()->toArray();

        return $result;
    }

    /**
     * 根据条件来获取列表的数据的总数
     * @param array $param
     */
    public function getCountsByCondition($map) {
        $join1 = [
            ['app_member_quan mq','mq.fu_id = f.fu_id'],
        ];

        $join2 = [
            ['app_member am','mq.member_id = am.id'],
        ];

        $order = ['f.fu_id' => 'desc'];

        $result =  $this->distinct(true)->field('f.*,am.tel,am.username,mq.addtime')
            ->alias('f')
            ->join($join1)
            ->join($join2)
            ->where($map)
            ->order($order)
            ->select();
        $result = count($result);
        return $result;
    }

    /**
     * 根据条件来获取列表的数据的总数
     * @param array $param
     */
    public function getCountByCondition($map = []) {
        return $this->where($map)->count();
    }

    /**
     * 插入福券分类信息
     * @param $param
     */
    public function add($param)
    {
        if(!is_array($param)) {
            exception('传递数据不合法');
        }

        $this->allowField(true)->isUpdate(false)->save($param);

        return $this->fu_id;
    }

    /**
     * 编辑景区信息
     * @param $param
     */
    public function edit($param)
    {
        if(!is_array($param)) {
            exception('传递数据不合法');
        }
        $this->allowField(true)->isUpdate(true)->save($param, ['fu_id' => $param['fu_id']]);

        return $this->fu_id;
    }

    /**
     * [deleteTrueData  删除数据]
     * @author [C。]
     * @param string $id
     * @return bool
     */
    public function delete($id = '')
    {
        if(empty($id))
            exception('传递数据不合法');

        $res = $this->where('jing_id','=',$id)->delete();

        if($res)
            return true;
        else
            return false;
    }

    /**
     * [getFind  获取数据]
     * @author [C。]
     * @param string $id
     * @return bool
     */
    public function getFind($data = '')
    {
        $order = ['fu_id' => 'desc'];
        $res = $this->where('code','like','%'.$data.'%')->order($order)->find();
        return $res;
    }
    /**
     * [getAllData  获取全部数据]
     * @author [C。]
     * @return array
     */

    public function getAllField($map=[],$id_name = 'id')
    {
        return $this->field('fu_id,status,code,type')->order([$id_name => 'desc'])->where($map)->select()->toArray();
    }
}